//
//  JSTCommentCell.m
//  KYKJPackDemo
//
//  Created by 01 on 17/5/19.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "JSTCommentCell.h"

@interface JSTCommentCell ()
/**
 头像
 */
@property (nonatomic, strong) UIImageView *iconView;

/**
 昵称
 */
@property (nonatomic, strong) UILabel *nameLabel;

/**
 内容
 */
@property (nonatomic, strong) M80AttributedLabel *contentLabel;

/**
 时间
 */
@property (nonatomic, strong) UILabel *timeLabel;

/**
 回复用户可点击的名称
 */
@property (nonatomic, strong) UIButton *replyNameBtn;

@property (nonatomic, strong) UIButton *userBtn;

@property (nonatomic, strong) NSString *personId;

@property (nonatomic, strong) NSString *userId;

@end

@implementation JSTCommentCell

- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        _iconView = [[UIImageView alloc] init];
        [self addSubview:_iconView];
        
        _nameLabel = [[UILabel alloc] init];
        [self addSubview:_nameLabel];
        
        _contentLabel = [[M80AttributedLabel alloc] init];
        [self addSubview:_contentLabel];
        
        _timeLabel = [[UILabel alloc] init];
        [self addSubview:_timeLabel];
        
        _replyNameBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_replyNameBtn];
        
        _userBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_userBtn];
    }
    return self;
}

- (void)createCommentContent:(JSTDynamicDetailsCommentModel *)model
{
    if (!_contentLabel)
    {
        _contentLabel = [[M80AttributedLabel alloc] init];
        [self addSubview:_contentLabel];
    }
    
    _personId = model.replyId;
    _userId = model.userId;
    
    CGFloat iconX = kCurrentScreenWidth(10);
    CGFloat iconY = iconX;
    CGFloat iconW = kCurrentScreenWidth(20);
    CGFloat iconH = iconW;
    _iconView.frame = CGRectMake(iconX, iconY, iconW, iconH);
    [_iconView setImageWithURL:[NSURL URLWithString:model.iconUrl]];
    _iconView.layer.masksToBounds = YES;
    _iconView.layer.cornerRadius = _iconView.width/2;
    
    CGFloat nameX = CGRectGetMaxX(_iconView.frame) + iconX;
    CGSize nameSize = [NSString sizeWithString:model.userNick font:kSystemFont(kCurrentScreenWidth(13)) CGsize:CGSizeMake(kScreenWidth - nameX - iconX, CGRectGetMaxY(_iconView.frame)+iconY)];
    CGFloat nameY = (CGRectGetMaxY(_iconView.frame) + iconY - nameSize.height)/2;
    _nameLabel.frame = (CGRect){{nameX, nameY}, nameSize};
    _nameLabel.text = model.userNick;
    _nameLabel.font = kSystemFont(kCurrentScreenWidth(13));
    
    CGSize timeSize = [NSString sizeWithString:model.timeStr font:kSystemFont(kCurrentScreenWidth(10)) CGsize:CGSizeMake(kScreenWidth, MAXFLOAT)];
    CGFloat timeX = kScreenWidth - iconX - timeSize.width;
    CGFloat timeY = (CGRectGetMaxY(_iconView.frame) + iconY - timeSize.height)/2.;
    _timeLabel.frame = (CGRect){{timeX, timeY}, timeSize};
    _timeLabel.text = model.timeStr;
    _timeLabel.font = kSystemFont(kCurrentScreenWidth(10));
    _timeLabel.textColor = [UIColor colorWithHexString:@"838383"];
    
    if ([model.replyId isEqualToString:@"0"]) {
        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:model.contentText];
        [_contentLabel appendAttributedText:attributedStr];
    } else {
        NSString *contentStr = [NSString stringWithFormat:@"回复 %@ %@", model.replyName, model.contentText];
        NSArray *components = [contentStr componentsSeparatedByString:@" "];
        for (int i = 0; i < components.count; i++) {
            NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:[components safeObjectAtIndex:i]];
            if (i == 1) {
                [attributedStr m80_setFont:kSystemFont(kCurrentScreenWidth(12))];
                [attributedStr m80_setTextColor:[UIColor blueColor]];
            } else {
                [attributedStr m80_setFont:kSystemFont(kCurrentScreenWidth(10))];
                [attributedStr m80_setTextColor:[UIColor blackColor]];
            }
            [_contentLabel appendAttributedText:attributedStr];
            [_contentLabel appendText:@" "];
        }
    }
    CGSize contentSize = [_contentLabel sizeThatFits:CGSizeMake(kScreenWidth - 2 * iconX - iconW, MAXFLOAT)];
    CGFloat contentX = CGRectGetMaxX(_iconView.frame);
    CGFloat contentY = CGRectGetMaxY(_iconView.frame) + 5;
    _contentLabel.frame = CGRectMake(contentX, contentY, round(contentSize.width), round(contentSize.height));
    
    if (![model.replyId isEqualToString:@"0"]) {
        CGFloat replyNameX = contentX + kCurrentScreenWidth(15);
        CGFloat replyNameY = contentY;
        CGSize replyNameSize = [NSString sizeWithString:model.replyName font:kSystemFont(kCurrentScreenWidth(12)) CGsize:CGSizeMake(kScreenWidth - 2 * iconX - 30, MAXFLOAT)];
        _replyNameBtn.frame = CGRectMake(replyNameX, replyNameY, replyNameSize.width + kCurrentScreenWidth(30), replyNameSize.height);
        [_replyNameBtn setBackgroundColor:[UIColor clearColor]];
        [_replyNameBtn addTarget:self action:@selector(changeUserPage:) forControlEvents:UIControlEventTouchUpInside];
    } else {
        _replyNameBtn.hidden = YES;
    }
    
    _cellHeight = CGRectGetMaxY(_contentLabel.frame) + 5;
    
    CGFloat userX = 0;
    CGFloat userY = 0;
    CGFloat userW = CGRectGetMaxX(_iconView.frame) + iconX;
    CGFloat userH = _cellHeight;
    _userBtn.frame = CGRectMake(userX, userY, userW, userH);
    [_userBtn setBackgroundColor:[UIColor clearColor]];
    [_userBtn addTarget:self action:@selector(changeUserPage:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)createPraiseContent:(JSTDynamicDetailsPraiseModel *)model
{
    if (_contentLabel) {
        [_contentLabel removeFromSuperview];
    }
    
    CGFloat iconX = kCurrentScreenWidth(10);
    CGFloat iconY = iconX;
    CGFloat iconW = kCurrentScreenWidth(20);
    CGFloat iconH = iconW;
    _iconView.frame = CGRectMake(iconX, iconY, iconW, iconH);
    [_iconView setImageWithURL:[NSURL URLWithString:model.iconUrl]];
    _iconView.layer.masksToBounds = YES;
    _iconView.layer.cornerRadius = _iconView.width/2;
    
    CGFloat nameX = CGRectGetMaxX(_iconView.frame) + iconX;
    CGSize nameSize = [NSString sizeWithString:model.userNick font:kSystemFont(kCurrentScreenWidth(13)) CGsize:CGSizeMake(kScreenWidth - nameX - iconX, CGRectGetMaxY(_iconView.frame) + iconY)];
    CGFloat nameY = (CGRectGetMaxY(_iconView.frame) + iconY - nameSize.height)/2;
    _nameLabel.frame = (CGRect){{nameX, nameY}, nameSize};
    _nameLabel.text = model.userNick;
    _nameLabel.font = kSystemFont(kCurrentScreenWidth(13));
    
    CGSize timeSize = [NSString sizeWithString:model.timeStr font:kSystemFont(kCurrentScreenWidth(10)) CGsize:CGSizeMake(kScreenWidth, MAXFLOAT)];
    CGFloat timeX = kScreenWidth - iconX - timeSize.width;
    CGFloat timeY = (CGRectGetMaxY(_iconView.frame) + iconY - timeSize.height)/2;
    _timeLabel.frame = (CGRect){{timeX, timeY}, timeSize};
    _timeLabel.text = model.timeStr;
    _timeLabel.font = kSystemFont(kCurrentScreenWidth(10));
    _timeLabel.textColor = [UIColor colorWithHexString:@"838383"];
    
    _cellHeight = CGRectGetMaxY(_iconView.frame) + iconY;
}

- (void)changeUserPage:(UIButton *)sender
{
    NSString *userId;
    if (sender == _userBtn) {
        userId = _userId;
    } else {
        userId = _personId;
    }
    if (_delegate && [_delegate respondsToSelector:@selector(changePersonPage:)])
    {
        [_delegate changePersonPage:userId];
    }
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}
@end
